package com.test40_JDBC;

import com.test40_JDBC.factory.ServiceFactory;
import com.test40_JDBC.service.IBatchService;
import com.test40_JDBC.vo.Dept;
import com.test40_JDBC.vo.Emp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 数据批量存储
 */
public class Demo07_Test {

    public static void main(String[] args) throws Exception {
        IBatchService batchService = ServiceFactory.getBatchService();
        List<Dept> deptList = init();
        try {
            batchService.deptBatch(deptList);
            batchService.empBatch(deptList);
            batchService.commit();
        } catch (Exception e) {
            e.printStackTrace();
            batchService.rollback();
        } finally {
            batchService.close();
        }
    }

    public static List<Dept> init() {
        List<Dept> depts = new ArrayList<>();
        Dept deptA = new Dept(10L, "技术部", "北京");
        Dept deptB = new Dept(20L, "产品部", "上海");
        Dept deptC = new Dept(30L, "教学部", "济南");
        Emp empA = new Emp("张三", 780.9);
        Emp empB = new Emp("李四", 880.9);
        Emp empC = new Emp("王五", 980.9);
        Emp empD = new Emp("赵六", 680.9);
        Emp empE = new Emp("孙七", 580.9);
        Emp empF = new Emp("钱八", 180.9);
        deptA.getEmps().add(empA);
        deptA.getEmps().add(empB);
        deptB.getEmps().add(empC);
        deptB.getEmps().add(empD);
        deptC.getEmps().add(empE);
        deptC.getEmps().add(empF);
        empA.setDept(deptA);
        empB.setDept(deptA);
        empC.setDept(deptB);
        empD.setDept(deptB);
        empE.setDept(deptC);
        empF.setDept(deptC);
        Collections.addAll(depts, deptA, deptB, deptC);
        return depts;
    }
}
